home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Sound / FreqForEverChange / FreqForEverChange.c next >
Encoding:
C/C++ Source or Header  |  1995-02-11  |  9.0 KB  |  391 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <Windows.h>
  11. #include    <OSUtils.h>
  12. #include    <Controls.h>
  13. #include    <desk.h>
  14. #include    <dialogs.h>
  15. #include    <Events.h>
  16. #include    <Memory.h>
  17. #include    <Menus.h>
  18. #include    <Notification.h>
  19. #include    <OSEvents.h>
  20. #include    <Packages.h>
  21. #include    <Palettes.h>
  22. #include    <Printing.h>
  23. #include    <Files.h>
  24. #include    <Resources.h>
  25. #include    <SANE.h>
  26. #include    <SegLoad.h>
  27. #include    <Sound.h>
  28. #include    <SoundInput.h>
  29. #include    <Slots.h>
  30. #include    <ToolUtils.h>
  31. #include    <StdIO.h>
  32. #include    <math.h>
  33. #include    <StandardFile.h>
  34.  
  35. extern _DataInit();
  36.  
  37. #define    TRUE            0xFF
  38. #define    FALSE            0
  39. #define    VERSION            1
  40. #define    SR_BIT            0
  41.  
  42. #define    appleID            128            
  43. #define    appleMenu        0
  44. #define    aboutMeCommand    1
  45.  
  46. #define    fileID            129
  47. #define    startCommand    1
  48. #define    printCommand    3
  49. #define    quitCommand     5
  50.  
  51. #define    editID            130
  52.  
  53. #define    aboutMeDLOG        128
  54. #define    okButton        1
  55. #define    authorItem        2
  56. #define    languageItem    3
  57.  
  58. long                Tick, WhereBoard;
  59. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  60. Rect                screenRect, BaseRect, TotalRect, minRect;
  61. WindowPtr            whichWindow, myWindow;
  62. CTabHandle            mycolors;
  63. PaletteHandle        srcPalette;
  64. MenuHandle            mymenu1, mymenu2, mymenu0;
  65. EventRecord         myEvent;
  66. Boolean                DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
  67. GDHandle            theGDevice;
  68. SysEnvRec            theWorld;
  69. OSErr                OSys;
  70. SpBlockPtr            mySpBlockPtr;
  71. SpBlock                mySpBlock;
  72.  
  73. #ifdef powerc
  74.    QDGlobals    qd;
  75. #endif
  76.  
  77. /*______________________________________________________*/
  78. /*                What ever You want                    */
  79. /*______________________________________________________*/
  80. void start()
  81. {
  82.     Handle            SoundData;
  83.     SndChannelPtr    chan;
  84.     OSErr            err;
  85.     SndCommand        mycmd;
  86.     KeyMap            myKeys;
  87.     
  88.     SoundData = GetResource ('snd ', 100);
  89.     if (ResError() != noErr || SoundData == nil)
  90.         Debugger();
  91.     HLock (SoundData);
  92.             
  93.     mycmd.cmd = soundCmd;
  94.     mycmd.param1 = 0;
  95.     mycmd.param2 = (long) *SoundData;
  96.     
  97.     chan = nil;
  98.     err = SndNewChannel (&chan, sampledSynth, 0, nil);
  99.     if (err != noErr)
  100.         Debugger();
  101.     
  102.     err = SndDoImmediate (chan, &mycmd);
  103.     if (err != noErr)
  104.         Debugger();
  105.  
  106.     mycmd.cmd = freqCmd;
  107.     mycmd.param1 = 0;
  108.     mycmd.param2 = 60;
  109.     
  110.     err = SndDoCommand (chan, &mycmd, FALSE);
  111.     if (err != noErr)
  112.         Debugger();
  113.  
  114.     do {
  115.         GetKeys (myKeys);
  116.         if (((myKeys[3] >> 6) & 1) != FALSE) {
  117.             mycmd.cmd = freqCmd;
  118.             mycmd.param1 = 0;
  119.             mycmd.param2 += 1;
  120.     
  121.             err = SndDoImmediate (chan, &mycmd);
  122.             if (err != noErr)
  123.                 Debugger();
  124.             }
  125.             
  126.         if (((myKeys[3] >> 5) & 1) != FALSE) {
  127.             mycmd.cmd = freqCmd;
  128.             mycmd.param1 = 0;
  129.             mycmd.param2 -= 1;
  130.     
  131.             err = SndDoImmediate (chan, &mycmd);
  132.             if (err != noErr)
  133.                 Debugger();
  134.             }
  135.             
  136.         } while (((myKeys[0] >> 20) & 1) == FALSE);
  137.     
  138.     mycmd.cmd = quietCmd;
  139.     mycmd.param1 = 0;
  140.     mycmd.param2 = 0;
  141.     
  142.     err = SndDoImmediate (chan, &mycmd);
  143.     if (err != noErr)
  144.         Debugger();
  145.  
  146.     err = SndDisposeChannel (chan,FALSE);
  147.     if (err != noErr)
  148.         Debugger();
  149.         
  150.     HUnlock (SoundData);
  151.     return;
  152. }
  153.  
  154. /*______________________________________________________*/
  155. /*                 About Prog Dialog                    */
  156. /*______________________________________________________*/
  157. void showAboutMeDialog()
  158. {
  159.     GrafPtr     savePort;
  160.     DialogPtr    theDialog;
  161.     short        itemHit;
  162.  
  163.     GetPort(&savePort);
  164.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  165.     SetPort(theDialog);
  166.  
  167.     do {
  168.         ModalDialog(nil, &itemHit);
  169.     } while (itemHit != okButton);
  170.  
  171.     CloseDialog(theDialog);
  172.  
  173.     SetPort(savePort);
  174.     return;
  175. }
  176.  
  177. /*______________________________________________________*/
  178. /*                 Do Menu Function                     */
  179. /*______________________________________________________*/
  180. void doCommand(mResult)
  181.     long    mResult;
  182. {
  183.     int                     theMenu, theItem;
  184.     char                    daName[256];
  185.     GrafPtr                 savePort;
  186.  
  187.     theItem = LoWord(mResult);
  188.     theMenu = HiWord(mResult);
  189.     
  190.     switch (theMenu) {
  191. /*______________________________________________________*/
  192. /*                    Do Apple Menu                     */
  193. /*______________________________________________________*/
  194.         case appleID:
  195.             if (theItem == aboutMeCommand)
  196.                 showAboutMeDialog();
  197.             else {
  198.                 GetItem(mymenu0, theItem, daName);
  199.                 GetPort(&savePort);
  200.                 (void) OpenDeskAcc(daName);
  201.                 SetPort(savePort);
  202.             }
  203.             break;
  204. /*______________________________________________________*/
  205. /*                     Do File Menu                     */
  206. /*______________________________________________________*/
  207.         case fileID:
  208.             switch (theItem) {
  209.                 case startCommand:
  210.                     start();
  211.                     break;
  212.                 case printCommand:
  213.                     break;
  214.                 case quitCommand:
  215.                     DoneFlag = TRUE;
  216.                     break;
  217.                 default:
  218.                     break;
  219.                 }
  220.             break;
  221. /*______________________________________________________*/
  222. /*                     Do Edit Menu                     */
  223. /*______________________________________________________*/
  224.         case editID:
  225.             switch (theItem) {
  226.                 default:
  227.                     break;
  228.                 }
  229.             break;
  230.         default:
  231.             break;
  232.         }
  233.     HiliteMenu(0);
  234.     return;
  235. }
  236.  
  237.  
  238. #ifdef powerc
  239.    QDGlobals    qd;
  240. #endif
  241.  
  242. /*______________________________________________________*/
  243. /*               Initialization traps                   */
  244. /*______________________________________________________*/
  245. void init()
  246. {
  247.     UnloadSeg(_DataInit);
  248.     InitGraf(&qd.thePort);
  249.     FlushEvents(everyEvent, 0);
  250.     InitWindows();
  251.     InitDialogs(nil);
  252.     InitCursor();
  253.     MaxApplZone();
  254. /*______________________________________________________*/
  255. /*            If not right Machine then stop            */
  256. /*______________________________________________________*/
  257.     OSys = SysEnvirons(VERSION,&theWorld);
  258.     if(!theWorld.hasColorQD)
  259.         Color = TRUE;
  260.     else
  261.         Color = FALSE;
  262.         
  263.     if(theWorld.processor >= env68020)
  264.         CPU20 = TRUE;
  265.     else
  266.         CPU20 = FALSE;
  267.         
  268.     if(!theWorld.hasFPU)
  269.         FPU = TRUE;
  270.     else
  271.         FPU = FALSE;
  272.         
  273.     if(theWorld.systemVersion < 0x0600)
  274.         Sys6 = FALSE;
  275.     else
  276.         Sys6 = TRUE;
  277.                     
  278. /*______________________________________________________*/
  279. /*                    Set menus                         */
  280. /*______________________________________________________*/
  281.     mymenu0 = GetMenu(appleID);
  282.     AddResMenu(mymenu0, 'DRVR');
  283.     InsertMenu(mymenu0,0);
  284.     mymenu1 = newmenu(129,"File");
  285.     appendmenu(mymenu1,"Start;-;Print;-;Quit");
  286.     InsertMenu(mymenu1,0);
  287.     mymenu2 = newmenu(130,"Edit");
  288.     InsertMenu(mymenu2,0);
  289.     DrawMenuBar();
  290.  
  291. /*______________________________________________________*/
  292. /*                  Init variables                      */
  293. /*______________________________________________________*/
  294.     DoneFlag = FALSE;
  295.     yieldTime = 0;
  296.     return;
  297. }
  298.  
  299. main()
  300. {
  301.     Boolean        track;
  302.     long        growResult;
  303. /*______________________________________________________*/
  304. /*                   Main Event loop                    */
  305. /*______________________________________________________*/
  306.     init();
  307.     for ( ;; ) {
  308.         if (DoneFlag) {
  309.             ExitToShell();
  310.             }
  311.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  312.             switch (myEvent.what) {
  313.                 case mouseDown:
  314.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  315.                         case inSysWindow:
  316.                             SystemClick(&myEvent, whichWindow);
  317.                             break;
  318.                         case inMenuBar:
  319.                             doCommand(MenuSelect(myEvent.where));
  320.                             break;
  321.                         case inContent:
  322.                             break;
  323.                         case inDrag:
  324.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  325.                             EraseRect (&whichWindow->portRect);
  326.                             DrawGrowIcon (whichWindow);
  327.                             break;
  328.                         case inGrow:
  329.                             growResult = GrowWindow (whichWindow, myEvent.where,
  330.                                                     &minRect);
  331.                             SizeWindow(whichWindow, LoWord(growResult), 
  332.                                     HiWord(growResult), TRUE);
  333.                             EraseRect (&whichWindow->portRect);
  334.                             DrawGrowIcon (whichWindow);
  335.                             break;
  336.                         case inGoAway:
  337.                             track = TrackGoAway (whichWindow, myEvent.where);
  338.                             if (track)
  339.                                 CloseWindow (whichWindow);
  340.                             break;
  341.                         case inZoomIn:
  342.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  343.                             if (track) {
  344.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  345.                                 EraseRect (&whichWindow->portRect);
  346.                                 DrawGrowIcon (whichWindow);
  347.                                 }
  348.                             break;
  349.                         case inZoomOut:
  350.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  351.                             if (track) {
  352.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  353.                                 EraseRect (&whichWindow->portRect);
  354.                                 DrawGrowIcon (whichWindow);
  355.                                 }
  356.                             break;
  357.                         default:
  358.                             break;
  359.                         }
  360.                     break;
  361.                 case keyDown:
  362.                     break;
  363.                 case autoKey:
  364.                     break;
  365.                 case updateEvt:
  366.                     if ((WindowPtr) myEvent.message == myWindow) {
  367.                         BeginUpdate((WindowPtr) myWindow);
  368.                         EndUpdate((WindowPtr) myWindow);
  369.                         }
  370.                     break;
  371.                 case diskEvt:
  372.                     break;
  373.                 case activateEvt:
  374.                     break;
  375.                 case app4Evt:
  376.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  377.                         yieldTime = 30;
  378.                         HideWindow((WindowPtr) myWindow);
  379.                         }
  380.                     else { /* Resume */
  381.                         yieldTime = 0;
  382.                         ShowWindow((WindowPtr) myWindow);
  383.                         SetPort((WindowPtr) myWindow);
  384.                         }
  385.                     break;
  386.                 default:
  387.                     break;
  388.                 }
  389.             }
  390.         }
  391. }